package com.xgk.boot.module.core.service.truck;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgk.boot.framework.common.constant.ErrorCodeConstants;
import com.xgk.boot.framework.common.exception.util.ServiceExceptionUtil;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xgk.boot.module.core.controller.admin.truck.vo.TruckOrderReqVO;
import com.xgk.boot.module.core.controller.admin.truck.vo.TruckOrderServiceFeeReqVO;
import com.xgk.boot.module.core.controller.admin.truck.vo.TruckOrderServiceFeeVO;
import com.xgk.boot.module.core.controller.admin.truck.vo.TruckOrderVO;
import com.xgk.boot.module.core.dal.entity.truck.TruckOrderAddressDO;
import com.xgk.boot.module.core.dal.entity.truck.TruckOrderServiceFeeDO;
import com.xgk.boot.module.core.dal.entity.truck.TruckOrdersDO;
import com.xgk.boot.module.core.dal.iservice.truck.TruckOrderServiceFeeService;
import com.xgk.boot.module.core.dal.mapper.truck.TruckOrderServiceFeeMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 服务商费用记录 服务实现
 */
@Service
public class TruckOrderServiceFeeServiceImpl extends ServiceImpl<TruckOrderServiceFeeMapper, TruckOrderServiceFeeDO> implements TruckOrderServiceFeeService {


    //分页查看订单(带查询)

    public PageResult<TruckOrderServiceFeeVO> pageTruckOrderServiceFeds(TruckOrderServiceFeeReqVO orderReqVO){
        if(orderReqVO == null){
            return new PageResult<>();
        }
        LambdaQueryWrapperX<TruckOrderServiceFeeDO> ordersWrapperX = new LambdaQueryWrapperX<>();
        ordersWrapperX.eqIfPresent(TruckOrderServiceFeeDO::getOrderCode, orderReqVO.getOrderCode());

        if ((orderReqVO.getBeginCreateTime() == null && orderReqVO.getEndCreateTime() != null) ||
                (orderReqVO.getBeginCreateTime() != null && orderReqVO.getEndCreateTime() == null)) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.COMMON_PARAMER_EXCEPTION,"beginCreateTime or endCreateTime");
        }

        ordersWrapperX.betweenIfPresent(TruckOrderServiceFeeDO::getCreateTime, orderReqVO.getBeginCreateTime(), orderReqVO.getEndCreateTime());
        ordersWrapperX.orderByDesc(TruckOrderServiceFeeDO::getCreateTime);

        Page<TruckOrderServiceFeeDO> page=new Page<>(orderReqVO.getCurrent(),orderReqVO.getPageSize());
        Page<TruckOrderServiceFeeDO> pageRes = this.page(page, ordersWrapperX);

        if(pageRes == null || CollUtil.isEmpty(pageRes.getRecords())){
            return new PageResult<>();
        }
        List<TruckOrderServiceFeeVO> truckOrderServiceFeeVOS = BeanUtils.toBean(pageRes.getRecords(), TruckOrderServiceFeeVO.class);
        return new PageResult<>(truckOrderServiceFeeVOS, pageRes.getTotal(), orderReqVO.getCurrent(), orderReqVO.getPageSize());
    }

    //获取单条记录详情
    public TruckOrderServiceFeeDO getServiceFeeByOrderCode(String orderCode){
        if(StrUtil.isBlank(orderCode)){
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.COMMON_PARAMER_EXCEPTION,orderCode);
        }
        LambdaQueryWrapperX<TruckOrderServiceFeeDO> ordersWrapperX = new LambdaQueryWrapperX<>();
        ordersWrapperX.eqIfPresent(TruckOrderServiceFeeDO::getOrderCode, orderCode);
        return this.getOne(ordersWrapperX);
    }

}
